﻿using System;
using System.Web;

using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Web.SessionState;

namespace IFD2
{
    public class flt_avail_fares : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");

            //if (IsPostBack)
            //{
            //    Response.Write("* Don't post back!");
            //    Response.End();
            //    return;
            //}

            if (context.Session["ACL"].ToString().IndexOf("SA3") < 0)
            {
                context.Response.Write("* Unauthorized!");
                context.ApplicationInstance.CompleteRequest();
                return;
            }
            //Response.Write("OK");
            //Response.End();
            //return;
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            StringBuilder buf = new StringBuilder();
            string fare_key;
            int ret_code;
            DataTable dt_fares = new DataTable();

            try
            {
                cmd.Parameters.Clear();
                cmd.CommandText = "bll_query_get_flt_avail_fares";
                para = cmd.Parameters.AddWithValue("@airline", context.Request.QueryString["airline"]);
                para = cmd.Parameters.AddWithValue("@flt_no", context.Request.QueryString["flt_no"]);
                para = cmd.Parameters.AddWithValue("@flt_date", context.Request.QueryString["flt_date"]);
                para = cmd.Parameters.AddWithValue("@ori_airport", context.Request.QueryString["ori_airport"]);
                para = cmd.Parameters.AddWithValue("@des_airport", context.Request.QueryString["des_airport"]);
                para = cmd.Parameters.AddWithValue("@request_type", context.Session["REQUEST_TYPE"].ToString());
                //para = cmd.Parameters.AddWithValue("@dep_time", Request.QueryString["dep_time"]);
                //para = cmd.Parameters.AddWithValue("@tax_type", context.Request.QueryString["tax_type"]);/*由于此处不查询税，所以暂时无需用到航班税类型,而且查询语句也关联了航班表*/
                para = cmd.Parameters.AddWithValue("@lang", context.Session["LANG"].ToString());
                para = cmd.Parameters.AddWithValue("@seats", context.Request.QueryString["seats"]);
                para = cmd.Parameters.AddWithValue("@seg_sn", context.Request.QueryString["seg_sn"]);
                para = cmd.Parameters.AddWithValue("@seg_total", context.Request.QueryString["seg_total"]);
                para = cmd.Parameters.AddWithValue("@user_airline", context.Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@office", context.Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", context.Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", context.Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd, ref dt_fares);
                if (ret_code != 0)
                {
                    if (ret_code == 1)
                    {
                        context.Response.Write("City timezone not found!");
                        context.ApplicationInstance.CompleteRequest();
                        return;
                    }
                    else if (ret_code == 2)
                    {
                        context.Response.Write("Check trip time!");
                        context.ApplicationInstance.CompleteRequest();
                        return;
                    }
                    else
                    {
                        context.Response.Write("Error " + ret_code.ToString());
                        context.ApplicationInstance.CompleteRequest();
                        return;
                    }
                }
                //Response.Write("Done:"+dt_fares.Rows.Count.ToString());
                //Response.End();
                //return;
                if (dt_fares.Rows.Count == 0)
                {
                    context.Response.Write("<table><tr><td width='50px'></td><td>No available seats or fares.</td></tr></table>");
                    context.ApplicationInstance.CompleteRequest();
                    return;
                }
                buf.Clear();
                buf.Append("<table>");
                foreach (DataRow row in dt_fares.Rows)
                {
                    fare_key = "AIRLINE=" + context.Request.QueryString["airline"].Trim() + "&FLT_NO=" + context.Request.QueryString["flt_no"] + "&FLT_DATE=" + context.Request.QueryString["flt_date"] + "&FARE_CODE=" + row["fare_code"].ToString().TrimEnd() + "&DBF=" + row["dbf"].ToString().TrimEnd()
                        + "&ORI_AIRPORT=" + context.Request.QueryString["ori_airport"].TrimEnd() + "&DES_AIRPORT=" + context.Request.QueryString["des_airport"].TrimEnd() + "&SEG_SN=" + row["flt_seg_sn"].ToString().TrimEnd()
                        + "&LANG=" + context.Session["LANG"].ToString().TrimEnd() + "&FLT_SURCHARGE_TYPE=" + row["surcharge_type"].ToString().TrimEnd();
                    buf.Append("<tr><td width='50px'></td><td width='100px'>" + row["cabin_name"].ToString() + "</td><td width='80px'>" + row["class"].ToString().TrimEnd() + row["class_avail"].ToString() + "/" + row["cabin"].ToString().TrimEnd() + row["cabin_avail"].ToString());
                    if (row["class_status"].ToString() == "R")
                        buf.Append("*");
                    //buf.Append("</td><td width='250px'><input type='radio' onclick=\"AjaxGetFareDetails(event,'FareDetails.aspx?" + fare_key + "')\"");
                    buf.Append("</td><td width='250px'><input type='radio' onclick=\"$('#ajax1').load('FareDetails.aspx?" + fare_key + "');$('#modXXX').modal('show');\"");
                    //buf.Append("</td><td width='150px'><input type='radio' onclick=\"$('#ajax1').load('Unauthorized.aspx');$('#modXXX').modal('show');\"");
                    buf.Append(" name='KEY" + context.Request.QueryString["seg_sn"] + "' value='" + context.Request.QueryString["flt_date"]
                        + "|" + context.Request.QueryString["airline"] + "|" + context.Request.QueryString["flt_no"] + "|" + context.Request.QueryString["ori_airport"] + "|" + context.Request.QueryString["des_airport"] + "|"
                        + row["cabin"] + "|" + row["class"] + "|" + row["fare_code"] + "|" + row["dbf"].ToString() + "|"
                        //+ "|" + context.Request.QueryString["international"] + "|"
                        + context.Request.QueryString["dep_time"] + "|" + context.Request.QueryString["arr_time"]);
                    //+ "|" + row["seg_sn"].ToString());
                    buf.Append("'/>");//<a href='FareDetails.aspx?" + fare_key + "'>
                    if (Convert.ToInt16(row["seg_total"]) > 1)
                        buf.Append("<span style=\"background:#90f090;\">");
                    buf.Append(row["fare_name"].ToString() + "(" + row["fare_code"].ToString().TrimEnd() + ")<!--/a-->");
                    if (Convert.ToInt16(row["seg_total"]) > 1)
                        buf.Append("</span>");
                    buf.Append("<!--/input--></td><td width='200px'>" + row["currency"].ToString() + Convert.ToSingle(row["adt_price"]).ToString() + "/" + Convert.ToSingle(row["chd_price"]).ToString() + "/" + Convert.ToSingle(row["inf_price"]).ToString() + "</td><td width='120px'>Days Ahead:" + row["dbf"].ToString());
                    buf.Append("</td><td width='30px'>" + row["flt_seg_sn"].ToString() + "/" + row["seg_total"].ToString() + "</td></tr>");
                }
                buf.Append("</table>");
                context.Response.Write(buf.ToString());
                context.ApplicationInstance.CompleteRequest();
            }
            catch (Exception ex)
            {
                if (context.Application["DEBUG"].ToString() == "1")
                {
                    context.Response.Write(ex.Message);
                    context.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    context.Response.Write("Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.");
                    context.ApplicationInstance.CompleteRequest();
                }
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}